/**
 * 
 */
package cn.gov.msa.ws;

import org.springframework.context.ApplicationContext;
import cn.gov.msa.model.Org;
import cn.gov.msa.util.InfoObjUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**   
* @Title: DictService.java 
* @Package cn.gov.msa.ws 
* @Description: TODO(用一句话描述该文件做什么) 
* @author 何宇江  
* @date 2018年3月12日 上午10:08:56 
*/
public class OrgService {
	
	public static String getChildOrg(ApplicationContext ctx,String params)throws Exception{
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
	    JSONObject jsonResult = new JSONObject();
		
		try{
			
			cn.gov.msa.service.OrgService orgService = (cn.gov.msa.service.OrgService) ctx.getBean("OrgService");  
			
			JSONObject jsonParams = JSONObject.fromObject(params);
			
			Org org = new Org();
			org.setOrgCode(jsonParams.getString("orgCode"));
	        
	        JSONArray jaResult = JSONArray.fromObject(orgService.queryNextOrg(org));
	        if(jaResult.size() > 0){
	        	result = 1;
	        	info = InfoObjUtil.Success_select_OK;
	        	jsonResult.put("datas", jaResult);
	        }
		}
		catch(Exception e){
			e.printStackTrace();
        	result = 2;
        	info = InfoObjUtil.Exption_msg;
		}
		

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);
		
		return jsonResult.toString();
	}
    
	/*
	 * 字典明细查询服务
	 * */
	public static String getorg(ApplicationContext ctx, String params) throws Exception {int result = 0;
		String info = InfoObjUtil.Error_NODATA;
	    JSONObject jsonResult = new JSONObject();
		
		try{
			
			cn.gov.msa.service.OrgService orgService = (cn.gov.msa.service.OrgService) ctx.getBean("OrgService");  
			
	        
			JSONObject jsonParams = JSONObject.fromObject(params);
			
			
			Org org = new Org();
			if(jsonParams.has("orglevel") && jsonParams.getString("orglevel").length() != 0)
				org.setOrgLevel(jsonParams.getInt("orglevel"));
			if(jsonParams.has("orgtype") && jsonParams.getString("orgtype").length() != 0)
				org.setOrgType(jsonParams.getInt("orgtype"));
			if(jsonParams.has("porgcode") && jsonParams.getString("porgcode").length() != 0)
				org.setPorgCode(jsonParams.getString("porgcode"));
	        
	        JSONArray jaResult = JSONArray.fromObject(orgService.selectOrg(org));

	        if(jaResult.size() > 0){
	        	result = 1;
	        	info = InfoObjUtil.Success_select_OK;
	        	jsonResult.put("datas", jaResult);
	        }
		}
		catch(Exception e){
			e.printStackTrace();
        	result = 2;
        	info = InfoObjUtil.Exption_msg;
		}
		

    	jsonResult.put("result", result);
    	jsonResult.put("info", info);

		
		return jsonResult.toString();
	}
	
	/**
	 * 获取常用机构
	 * @param ctx
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String getFrequentlyOrg(ApplicationContext ctx, String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {

			cn.gov.msa.service.OrgService orgService = (cn.gov.msa.service.OrgService) ctx.getBean("OrgService");

			JSONObject jsonParams = JSONObject.fromObject(params);
			
			JSONArray jaResult = JSONArray.fromObject(orgService.selectFrequentlyOrg(jsonParams.getString("companyid")));

			if (jaResult.size() > 0) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", jaResult);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	/**
	 * 获取机构(旧)
	 * @param ctx
	 * @param params
	 * @return
	 * @throws Exception
	 */
	public static String getOrg1(ApplicationContext ctx, String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {

			JSONObject jsonParams = JSONObject.fromObject(params);

			cn.gov.msa.service.OrgService orgService = (cn.gov.msa.service.OrgService) ctx.getBean("OrgService");

			Org org = new Org();
			if(jsonParams.has("orglevel") && jsonParams.getString("orglevel").length() != 0)
				org.setOrgLevel(jsonParams.getInt("orglevel"));
			if(jsonParams.has("orgtype") && jsonParams.getString("orgtype").length() != 0)
				org.setOrgType(jsonParams.getInt("orgtype"));
			if(jsonParams.has("porgcode") && jsonParams.getString("porgcode").length() != 0)
				org.setPorgCode(jsonParams.getString("porgcode"));
	        
	        JSONArray jaResult = JSONArray.fromObject(orgService.selectOrg(org));
	        JSONObject json = new JSONObject();
	        if (jaResult.size() > 0) {
	        	JSONArray orgList = new JSONArray();
	        	for(int i = 0; i < jaResult.size(); i++){
	        		JSONObject porg = new JSONObject();
	        		porg.put("id", jaResult.getJSONObject(i).getString("orgCode"));
	        		porg.put("name", jaResult.getJSONObject(i).getString("orgName"));
	        		// 搜索下级机构
	        		org.setOrgCode(jaResult.getJSONObject(i).getString("orgCode"));
	        		JSONArray childOrgResult = JSONArray.fromObject(orgService.queryNextOrg(org));
	        		if(childOrgResult.size() > 0){
	        			JSONObject childOrg = new JSONObject();
	        			JSONArray childOrgList = new JSONArray();
	        			for (int j = 0; j < childOrgResult.size(); j++) {
	        				childOrg.put("id", childOrgResult.getJSONObject(j).getString("orgCode"));
	        				childOrg.put("name", childOrgResult.getJSONObject(j).getString("orgName"));
	        				childOrgList.add(childOrg);
	        			}
	        			porg.put("city", childOrgList);
	        		}
	        		orgList.add(porg);
	        	}
	        	json.put("province", orgList);
	        }
			
			// 获取常用机构
	        JSONArray hotOrgList = new JSONArray();
	        if(jsonParams.has("companyid")){
	        	JSONArray hotOrgResult = JSONArray.fromObject(orgService.selectFrequentlyOrg(jsonParams.getString("companyid")));
	        	if(hotOrgResult.size() > 0){
	        		for (int i = 0; i < hotOrgResult.size(); i++) {
	        			JSONObject hotOrg = new JSONObject();
	        			hotOrg.put("id", hotOrgResult.getJSONObject(i).getString("orgCode"));
	        			hotOrg.put("name", hotOrgResult.getJSONObject(i).getString("orgName"));
	        			hotOrgList.add(hotOrg);
	        		}
	        	}
	        }
	        json.put("hot", hotOrgList);

			if (!json.isEmpty() || !json.isNullObject()) {
				result = 1;
				info = InfoObjUtil.Success_select_OK;
				jsonResult.put("datas", json);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
	}
	
	public static String getOrg(ApplicationContext ctx, String params) throws Exception {
		int result = 0;
		String info = InfoObjUtil.Error_NODATA;
		JSONObject jsonResult = new JSONObject();

		try {

			JSONObject jsonParams = JSONObject.fromObject(params);

			cn.gov.msa.service.OrgService orgService = (cn.gov.msa.service.OrgService) ctx.getBean("OrgService");

			Org org = new Org();
			if(jsonParams.has("orglevel") && jsonParams.getString("orglevel").length() != 0)
				org.setOrgLevel(jsonParams.getInt("orglevel"));
			if(jsonParams.has("orgtype") && jsonParams.getString("orgtype").length() != 0)
				org.setOrgType(jsonParams.getInt("orgtype"));
			if(jsonParams.has("porgcode") && jsonParams.getString("porgcode").length() != 0)
				org.setPorgCode(jsonParams.getString("porgcode"));
	        
			// 获取所有机构
	        JSONArray orgResult = JSONArray.fromObject(orgService.selectAllOrg(org));
	        
			// 获取常用机构
	        JSONArray hotOrgResult = new JSONArray();
	        if(jsonParams.has("companyid")){
	        	hotOrgResult = JSONArray.fromObject(orgService.selectFrequentlyOrg(jsonParams.getString("companyid")));
	        }
	        
	        JSONObject json = new JSONObject();
	        json.put("hisResult", hotOrgResult);
	        json.put("result", orgResult);

	        result = 1;
			info = InfoObjUtil.Success_select_OK;
			jsonResult.put("datas", json);
		} catch (Exception e) {
			e.printStackTrace();
			result = 2;
			info = InfoObjUtil.Exption_msg;
		}

		jsonResult.put("result", result);
		jsonResult.put("info", info);

		return jsonResult.toString();
		
	}
}
